﻿using MediatR;
using Traceability_Management.Domain;
using Traceability_Management.ErrorCode;
using Traceability_Management.Infrastructure.Interfaces;
using Traceability_Management.Read.Api.Application.Command.HealthyWay;

namespace Traceability_Management.Read.Api.Application.CommandHandler.HealthyWay
{
    public class AddHealthysCommandHandler : IRequestHandler<AddHealthysCommand, ApiResult<int>>
    {
        IBaseReponsitory<Healthys> Healthys;

        public AddHealthysCommandHandler(IBaseReponsitory<Healthys> Healthys)
        {
            this.Healthys = Healthys;
        }

        public async Task<ApiResult<int>> Handle(AddHealthysCommand request, CancellationToken cancellationToken)
        {
            var list = Healthys.GetQuery().Where(c => c.Healthys_Way == request.Healthys.Healthys_Way && !c.IsDel).ToList();
            if (list.Count > 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "该方式不可重复",
                    Data = -1
                };
            }
            await Healthys.AddAsync(request.Healthys);
            return new ApiResult<int> 
            {
                Code=ApiEnum.Success,
                Message="添加成功",
                Data=1
            };
        }
    }
}
